using System.Collections;
using System.Collections.Generic;
using Framework.Library.Core.Config;
using Framework.Library.Core.Data.DataEngine;
using Framework.Library.Data.DAL;
using Framework.Library.Data.IDAO;
using Framework.Library.Core.Utils.Database;
using System.Data;
using System.Text;
using System;

namespace Framework.Library.Data.DAO.SqlServer
{
    [Serializable]
    internal class PortalRoleManager : IPortalRoleManager
    {
        private Configurations cfg = new Configurations();
        #region IPortalRoleManager Members

        public List<PortalRole> GetRoles(int portalId)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_ROLES");
            builder.AddCondition("PORTALID", portalId);
            return GeneralConnection.ExecuteList<PortalRole>(CommandType.Text, builder);
        }

        public PortalRole GetRole(int userId, int portalId)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_ROLES");
            builder.AddCondition("USERID", userId);
            builder.AddCondition("PORTALID", portalId);
            builder.AddTable("PORTAL_ROLES", new List<string>() { "ROLEID" }, "PORTAL_USERSROLES", new List<string>() { "ROLEID" }, JoinTypes.InnerJoin);
            List<PortalRole> lst = GeneralConnection.ExecuteList<PortalRole>(CommandType.Text, builder);
            if (lst.Count > 0)
                return lst[0];
            return null;
        }

        public void Delete(int roleId)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_ROLES");
            StringBuilder sb = new StringBuilder();
            sb.Append("DELETE " + Configurations.DATABASEOWNER + "Portal_Roles WHERE RoleID = @RoleID\t");
            sb.Append("DELETE " + Configurations.DATABASEOWNER + "PORTAL_USERSROLES WHERE ROLEID = @ROLEID\t");
            builder.Query = sb.ToString();
            builder.AddParameter("RoleID", roleId);
            builder.QueryType = QueryTypes.TSql;
            GeneralConnection.ExecuteNonQuery(CommandType.Text, builder);
        }

        public void AddUserToRole(int userId, int roleId)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_USERSROLES");
            builder.QueryType = QueryTypes.Insert;
            builder.AddNameValuePair("UserID", userId);
            builder.AddNameValuePair("RoleID", roleId);
            GeneralConnection.ExecuteNonQuery(CommandType.Text, builder);
        }

        public void RemoveUserFromRole(int userId, int roleId)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_USERSROLES");
            builder.QueryType = QueryTypes.Delete;
            builder.AddNameValuePair("UserID", userId);
            builder.AddNameValuePair("RoleID", roleId);
            GeneralConnection.ExecuteNonQuery(CommandType.Text, builder);
        }

        public void UpdatePermission(int roleId, string Permission)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_ROLES");
            builder.QueryType = QueryTypes.Update;
            builder.AddCondition("ROLEID", roleId);
            builder.AddNameValuePair("PERMISIONS", Permission);
            GeneralConnection.ExecuteNonQuery(CommandType.Text, builder);
        }

        #endregion
    }
}